
/**
 * 
 * @param {*} wktStr 
 * @returns 返回起终点坐标
 */
export const getXYBywktLine = wktStr => {
    wktStr = wktStr.split('((')[1]
    wktStr = wktStr.split('))')[0]
    wktStr = wktStr.split(',')
    const fristPoint = wktStr[0].split(' ')
    const lastPoint = wktStr[1].split(' ')
    return [[parseFloat(fristPoint[0]), parseFloat(fristPoint[1])], [parseFloat(lastPoint[0]), parseFloat(lastPoint[1])]]

}

/**
 * 
 * @param {*} wktStr 
 * @returns 返回坐标数组
 */
export const getXYBywktPoint = wktStr => {
    wktStr = wktStr.split('(')[1]
    wktStr = wktStr.split(' ')
    return [parseFloat(wktStr[0]), parseFloat(wktStr[1])]
}

/**
 * 
 * @param {*} wktStr 
 * @returns 返回Z值
 */
export const getZBywktPoint = wktStr => {
    wktStr = wktStr.split('(')[1]
    wktStr = wktStr.split(' ')
    return parseFloat(wktStr[2])
}

export const getPointsOnCircle = (centerX, centerY, radius) => {
    const numberOfPoints = 24;
    const points = [];

    for (let i = 0; i < numberOfPoints; i++) {
        const angle = Math.PI * 2 / numberOfPoints * i;
        const x = centerX + Math.cos(angle) * radius;
        const y = centerY + Math.sin(angle) * radius;
        points.push([x, y]);
    }
    points.push([centerX + radius, centerY])

    return points;
}


/**
 * 经纬度转tms行列号
 * @param {*} lng 
 * @param {*} lat 
 * @param {*} zoom 
 * @returns 
 */
export const lnglat2tms = (lng, lat, zoom) => {
    const zoomImage = Math.ceil(zoom + 1)
    var tmsx = parseInt((lng + 180) / 360 * Math.pow(2, zoomImage));
    var latRad = lat * Math.PI / 180;
    var tmsy = (2 ** zoomImage - 1) - parseInt((1 - Math.log(Math.tan(latRad) + 1 / Math.cos(latRad)) / Math.PI) / 2 * Math.pow(2, zoomImage));
    return [tmsx, tmsy]
}

/**
 * 通过坐标获取不同层级的url
 * @param {*} lng 
 * @param {*} lat 
 * @param {*} minZoom 
 * @param {*} tmsUrl 
 * @returns 
 */
export const getTmsUrlByCoor = (lng, lat, minZoom, tmsUrl) => {
    var url = new Set()
    for (minZoom; minZoom <= 22; minZoom++) {
        const rowCell = lnglat2tms(lng, lat, minZoom - 1)
        var path = tmsUrl.replace("{z}", minZoom).replace("{x}", rowCell[0].toString()).replace("{y}", rowCell[1].toString())
        url.add(path)
    }
    return url

}

/**
 * 将GeoJSON对象转换为WKT格式的字符串
 * @param {Object} geojson - GeoJSON对象
 * @returns {string} WKT格式的字符串
 */
export const geojsonToWKT = (geojson) => {
    switch (geojson.type) {
        case "Point":
            return "POINT (" + geojson.coordinates.join(" ") + ")";
        case "LineString":
            return "LINESTRING (" + geojson.coordinates.map(coord => coord.join(" ")).join(",") + ")";
        case "Polygon":
            return "POLYGON ((" + geojson.coordinates[0].map(coord => coord.join(" ")).join(",") + "))";
        case "MultiPoint":
            return "MULTIPOINT (" + geojson.coordinates.map(coord => "(" + coord.join(" ") + ")").join(",") + ")";
        case "MultiLineString":
            return "MULTILINESTRING (" + geojson.coordinates.map(coords => "(" + coords.map(coord => coord.join(" ")).join(",") + ")").join(",") + ")";
        case "MultiPolygon":
            return "MULTIPOLYGON (" + geojson.coordinates.map(polygonCoords => "((" + polygonCoords.map(coord => coord.join(" ")).join(",") + "))").join(",") + ")";
        default:
            throw new Error("Invalid GeoJSON type: " + geojson.type);
    }
}

/**
 * 第一个extent是否包含第二extent
 */
export const extentIsContainsExtent = (extent, viewBox) => {
    const bool = extent.min_X < viewBox.min_X && extent.max_X > viewBox.max_X && extent.min_Y < viewBox.min_Y && extent.max_Y > viewBox.max_Y
    return bool
}